﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Portal.Utility;
using Portal.Web.Model;
using Portal.Web.Utility;
using Portal.Exam.Bussiness;
using Portal.Core;

namespace Portal.Exam.Render
{
    public class AddExamSegment
    {
        public static AjaxOut Draw(string SiteId)
        {
            AjaxPro.Utility.RegisterTypeForAjax(typeof(AddExamSegment));
            SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
            string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);

            string AccountType = WebEnvironments.Request(SiteId, "AccountType");
            if (string.IsNullOrEmpty(AccountType)) AccountType = "";

            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                WebSessionUtility.CheckSessionTimeOut();
                string BackUrl = "ExamSegment.aspx";
                string SelectType =
                    "<select id=drpSelectExamType>\r\n" +
                    "   <option value=\"exam\">Thi thật</option>\r\n" +
                    "   <option value=\"test\">Thi thử</option>\r\n" +
                    "</select>\r\n";
                StringBuilder RetHtml = new StringBuilder();
                RetHtml.Append(
                    "<input type=hidden id=txtAccountType value=\"" + AccountType + "\">\r\n" +
                    "<script language=javascript>\r\n" +
                    " function ClientSaveAddExamSegment()\r\n" +
                    "     {\r\n" +
                    "         SiteId='" + SiteId + "';\r\n" +
                    "         AccountType=document.getElementById(\"txtAccountType\").value;\r\n" +
                    "         ExamSegmentName=document.getElementById(\"txtExamSegmentName\").value;\r\n" +
                    "         FromDate=document.getElementById(\"txtFromDate\").value;\r\n" +
                    "         ToDate=document.getElementById(\"txtToDate\").value;\r\n" +
                    "         Year=document.getElementById(\"txtYear\").value;\r\n" +
                    "         ExamType=document.getElementById(\"drpSelectExamType\").value;\r\n" +
                    "         RetValue = Portal.Exam.Render.AddExamSegment.ServerSideAddExamSegment(SiteId,AccountType, ExamSegmentName,Year, FromDate, ToDate,  ExamType).value;\r\n" +
                    "         if(RetValue.indexOf(\"ERROR\")!=-1)\r\n" +
                    "         {\r\n" +
                    "             alert(RetValue);\r\n" +
                    "             return;\r\n" +
                    "         }\r\n" +
                    "         window.open('" + BackUrl + "','_self');\r\n" +
                    "     }\r\n" +

                    "</script>\r\n" +
                " <div class=DatabaseFunction>\r\n" +
                "        <ul>\r\n" +
                "            <li onclick=\"javascript:ClientSaveAddExamSegment();\"><img class=image src=\"" + IconRoot + "/Save.png\"><div>Ghi lại</div></li>\r\n" +
                "            <li onclick=\"javascript:window.open('" + BackUrl + "','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                "        </ul>\r\n" +
                "  </div>\r\n" +
                "   <h3>Thêm mới kỳ thi</h3>\r\n" +
                "   <div class=CssFormSaveUpdate>\r\n" +
                "     <table style=\"width:99%\">" +
                "          <tr>" +
                "              <td class=\"Col1\">Mô tả:</td>" +
                "              <td><input class=textbox id=txtExamSegmentName></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Từ ngày:</td>" +
                "              <td>" + LookUpUtilities.GenerateLookUpDate("txtFromDate", System.DateTime.Now) + "</td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Đến ngày</td>" +
                "              <td>" + LookUpUtilities.GenerateLookUpDate("txtToDate", System.DateTime.Now) + "</td>" +
                "          </tr>" +

                "          <tr>" +
                "              <td class=\"Col1\">Năm</td>" +
                "              <td><input type=textbox class=textbox id=txtYear value=\"" + System.DateTime.Now.Year + "\"></td>" +
                "          </tr>" +
                "          <tr>" +
                "              <td class=\"Col1\">Kiểu:</td>" +
                "              <td>" + SelectType + "</td>" +
                "          </tr>" +
                "      </table>" +
                "</div>\r\n");


                OAjaxOut.HtmlContent = RetHtml.ToString();
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod]
        public string ServerSideAddExamSegment(
            string SiteId,
            string AccountType,
            string ExamSegmentName,
            string Year,
            string FromDate,
            string ToDate,
            string ExamType)
        {
            try
            {
                WebSessionUtility.CheckSessionTimeOut();

                if (string.IsNullOrEmpty(ExamSegmentName))
                {
                    throw new Exception("Chưa nhập thông tin kỳ thi!");
                }
                if (!FunctionUtilities.checkVnDate(FromDate))
                {
                    throw new Exception("Thời gian nhập không hợp lệ!");
                }
                if (!FunctionUtilities.checkVnDate(ToDate))
                {
                    throw new Exception("Thời gian nhập không hợp lệ!");
                }
                if (!FunctionUtilities.checkInteger(Year))
                {
                    throw new Exception("Năm nhập không hợp lệ!");
                }

                DateTime dFromDate = FunctionUtilities.VNDateToDate(FromDate);
                DateTime dToDate = FunctionUtilities.VNDateToDate(ToDate);
                string ExamSegmentId = new ExamSegmentBll().CreateExamSegment(null, SiteId,AccountType, ExamSegmentName, int.Parse(Year), ExamType, dFromDate, dToDate);
                return "Thêm mới thành công!";
            }
            catch (Exception ex)
            {
                return "ERROR: " + ex.Message.ToString();
            }
        }

    }
}